﻿using System;
using System.Data;
using System.Data.SqlClient;
using vtech_frameWorks.DAO;
using vtech_frameWorks.Exceptions;
using vtech_system.Object;

namespace vtech_system.Menu
{
    public class ClsMenuDAO:ClsObjectDAO
    {
        public override System.Data.DataTable GetAll(System.Data.SqlClient.SqlTransaction sqlTransaction)
        {
            try
            {
                return ClsBaseDbAccessDAO.ExecuteSqlDataAdapter(sqlTransaction, "spGetMenu").Tables[0];
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                return null;
            }
        }

        public override void Insert(System.Data.SqlClient.SqlTransaction sqlTran, System.Data.DataRow dRow)
        {
            try
            {
                SqlParameter[] paramArray = new SqlParameter[]
                {
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@MenuID", SqlDbType.UniqueIdentifier, 0, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@MenuPosition", SqlDbType.Int, 0, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@MenuValue", SqlDbType.NVarChar, 255, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@ParentID", SqlDbType.UniqueIdentifier, 0, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@FormName", SqlDbType.NVarChar, 100, ParameterDirection.Input)
                };
                ClsBaseDbAccessDAO.SetParameterValues(paramArray, dRow);
                ClsBaseDbAccessDAO.ExecuteProcedure(sqlTran, "spAddMenu", paramArray);
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                return;
            }
        }

        public override void Edit(SqlTransaction sqlTran, DataRow dRow)
        {
            try
            {
                SqlParameter[] paramArray = new SqlParameter[]
                {
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@MenuID", SqlDbType.UniqueIdentifier, 0, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@MenuPosition", SqlDbType.Int, 0, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@MenuValue", SqlDbType.NVarChar, 255, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@ParentID", SqlDbType.UniqueIdentifier, 0, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@FormName", SqlDbType.NVarChar, 100, ParameterDirection.Input)
                };
                ClsBaseDbAccessDAO.SetParameterValues(paramArray, dRow);
                ClsBaseDbAccessDAO.ExecuteProcedure(sqlTran, "spEditMenu", paramArray);
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                return;
            }
        }

        public override void Delete(SqlTransaction sqlTransaction, DataRow dataRow)
        {
            try
            {
                SqlParameter[] paramArray = new SqlParameter[]
                {
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@MenuID", SqlDbType.UniqueIdentifier, 0, ParameterDirection.Input),
                };
                ClsBaseDbAccessDAO.SetParameterValues(paramArray, dataRow);
                ClsBaseDbAccessDAO.ExecuteNonQuery(sqlTransaction, "DELETE FROM [tblMenu] WHERE [MenuID] = @MenuID", paramArray);
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                return;
            }
        }
    }
}
